# -*- coding:utf-8 -*-
import yaml
import pymysql
import os



def read_db_config():
    os.path.dirname(__file__)
    file_path = os.path.split(os.path.realpath(__file__))[0]
    file_name_path = os.path.join(file_path,'config.yaml')
    f = open(file_name_path,'r',encoding='utf-8')
    cont = f.read()
    x = yaml.load(cont,Loader=yaml.FullLoader)
    return x['DB']['host'], x['DB']['username'], x['DB']['password'], x['DB']['port'], x['DB']['database']


def get_mydb():
    host,user,passwd,port,database = read_db_config()
    #print(host,user,passwd,port,database)
    mydb = pymysql.connect(
        host=host,
        user=user,
        passwd=passwd,
        database=database,
        port=port
    )
    mycursor = mydb.cursor()
    return mydb, mycursor

def select_udp_multicast_send_id():
    mydb, mycursor = get_mydb()
    sql = "select max(id) from udp_multicast_send"
    mycursor.execute(sql)
    ret = mycursor.fetchall()
    mycursor.close()
    mydb.close()
    return int(ret[0][0])

def select_udp_multicast_receiver_id():
    mydb, mycursor = get_mydb()
    sql = "select max(id) from udp_multicast_receiver"
    mycursor.execute(sql)
    ret = mycursor.fetchall()
    mycursor.close()
    mydb.close()
    return int(ret[0][0])

def select_max_receiver_dev():
    mydb, mycursor = get_mydb()
    sql = "select receiver_dev  from udp_multicast_receiver where id = (select max(id) from udp_multicast_receiver)"
    mycursor.execute(sql)
    ret = mycursor.fetchall()
    mycursor.close()
    mydb.close()
    return str(ret[0][0])

def insert_udp_multicast_send(multicast_group_addr, multicast_group_port, source_addr, source_port, source_dev, source_data,enable):
    mydb, mycursor = get_mydb()
    sql = "insert into udp_multicast_send (multicast_group_addr, multicast_group_port, source_addr, source_port, source_dev, source_data,  enable) VALUES (%s, %s, %s, %s, %s, %s, %s)"
    val = (multicast_group_addr, multicast_group_port, source_addr, source_port, source_dev, source_data,enable)
    mycursor.execute(sql, val)
    mydb.commit()

def insert_udp_multicast_receiver(multicast_group_addr, receiver_addr, receiver_port, receiver_dev):
    mydb, mycursor = get_mydb()
    val = multicast_group_addr, receiver_addr, receiver_port, receiver_dev
    sql = "insert into udp_multicast_receiver (multicast_group_addr, receiver_addr, receiver_port, receiver_dev) VALUES (%s, %s, %s, %s)"
    mycursor.execute(sql, val)
    mydb.commit()
    mycursor.close()
    mydb.close()

def update_udp_multicast_receiver(multicast_group_addr, receiver_addr, receiver_port, receiver_dev):
    mydb, mycursor = get_mydb()
    val = multicast_group_addr, receiver_addr, receiver_port, receiver_dev
    sql = "update udp_multicast_receiver set multicast_group_addr = %s,receiver_addr = %s,receiver_port = %s,receiver_dev = %s where receiver_dev = 'unknown' order by id desc limit 1 "
    mycursor.execute(sql, val)
    mydb.commit()
    mycursor.close()
    mydb.close()

def update_receiver_dev():
    mydb, mycursor = get_mydb()
    sql = "update udp_multicast_receiver set receiver_dev = 'empty' where receiver_dev = 'unknown' order by id desc limit 1 "
    mycursor.execute(sql)
    mydb.commit()
    mycursor.close()
    mydb.close()

def insert_udp_multicast_traffic(flag, byte, pkts, tm, udp_multicast_id):
    mydb, mycursor = get_mydb()
    sql = "insert into udp_multicast_traffic (flag, byte, pkts, tm, udp_multicast_id) VALUES (%s, %s, %s, %s, %s)"
    val = flag, byte, pkts, tm, udp_multicast_id
    mycursor.execute(sql, val)
    mydb.commit()
    mycursor.close()
    mydb.close()

def insert_udp_multicast_traffic_speed(flag, speed, tm, udp_multicast_id):
    mydb, mycursor = get_mydb()
    sql = "insert into udp_multicast_traffic_speed (flag, speed, tm, udp_multicast_id) VALUES (%s, %s, %s, %s)"
    val = flag, speed, tm, udp_multicast_id
    mycursor.execute(sql, val)
    mydb.commit()
    mycursor.close()
    mydb.close()

    
